<template>
  <div class="reportFormPage">
    <!--    <topOperation/>-->
    <div class="report-dom">
      <el-row>
        <el-col :span="5" :offset="19">
          <div class="pageNumBox">
            <span>第</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
            <span>页，共</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
            <span>页</span>
          </div>
        </el-col>
      </el-row>
      <div class="topTwoBox">
        <div class="centerName">土承载比（CBR）试验检测记录表(三）</div>
        <div class="codeText">
          <tr>JGLQ01008</tr>
        </div>
      </div>
      <div class="threeTopBox">
        <div class="left-border">
          <div class="box-title">检测单位名称：</div>
          <div class>{{ form.jiancdwmc }}</div>
        </div>
        <div class="right-border">
          <span>记录编号：</span>
          <span class="placeholder">{{ form.jilbh }}</span>
        </div>
      </div>
      <div class="table-border">
        <!-- .........................1111111111111111111111111111111......................................................... -->
        <table height="220px" class="top-table" align="center">
          <tbody>
            <tr>
              <td height="30" align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程名称</span>
              </td>
              <td width="35%" class="text-center">
                <span>{{ form.gongcmc }}</span>
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程部位/用途</span>
              </td>
              <td width="35%" class="text-center">
                <span>{{ form.gongcbwyt }}</span>
              </td>
            </tr>
            <tr>
              <td height="50" align="center">样品信息</td>
              <td class="specimen-border" colspan="3">
                <span>样品名称：</span>
                <span :class="{ 'placeholder': !form.yangpmc }">{{ form.yangpmc }}</span>
                <span>；样品编号：</span>
                <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>

                <span>；样品数量：</span>
                <span :class="{ 'placeholder': !form.yangpsl }">{{ form.yangpsl }}</span>
                <span>；样品状态：</span>
                <span :class="{ 'placeholder': !form.yangpzt }">{{ form.yangpzt }}</span>
                <span>；来样时间：</span>
                <span :class="{ 'placeholder': !form.yangpsj }">{{ form.yangpsj }}</span>
              </td>
            </tr>

            <tr>
              <td height="25" align="center" width="15%">
                <span style="width: 90px; display: inline-block">试验检测日期</span>
              </td>
              <!-- <td width="35%" class="text-center">{{ form.shiyjcrq }}</td> -->
              <td width="35%" class="text-center">
                <el-input v-model="form.shiyjcrq" />
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">试验条件</span>
              </td>
              <td v-if="isPdf" width="35%">
                温度：
                <span style="display: inline-block; width: 10%;">{{ form.shiytjwd }}</span>
                ℃；湿度：
                <span style="display: inline-block; width: 10%;">{{ form.shiytjsd }}</span>%RH
              </td>
              <td v-else width="35%">
                温度：
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjwd" class="height100"
                  style="width: 15%" />℃；湿度：
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjsd" class="height100"
                  style="width: 15%" />%RH
              </td>
            </tr>

            <tr>
              <td height="50" align="center" width="15%">
                <span style="width: 90px; display: inline-block">检测依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.jiancyj }}</span>
                <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark"
                  @dblclick.native="showBsJiancyjView" readonly />
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">判定依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.pandyj }}</span>
                <el-input v-else type="textarea" v-model="form.pandyj" :rows="2" @dblclick.native="showBsJudgeBaseView"
                  @focus="handleFocus('pandyj')" class="mark" />
              </td>
            </tr>
            <tr class="bottom-border">
              <td height="50" align="center">
                <div>主要仪器设备</div>
                <div>名称及编号</div>
              </td>
              <td colspan="3">
                <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh" class="height100 width100 mark"
                  @dblclick.native="showBsInstrumentView" readonly></el-input>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .................................22222222222222222222222222222222............................................... -->
        <table height="616px" class="middle-table">
          <tbody>
            <tr trindex="0">
              <td colspan="4" class="td4">最大干密度(g/cm3)</td>
              <td colspan="4" class="td4">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.zuidgmd" disabled />
              </td>
              <td colspan="4" class="td4">最佳含水率（%）</td>
              <td colspan="4" class="td4">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.zuijhsl" disabled />
              </td>
            </tr>
            <tr trindex="1">
              <td colspan="1" class="td1">
                试件
                <br />编号
              </td>
              <td colspan="3" class="td3">击实次数</td>
              <td colspan="6" class="td6">压实度（%）</td>
              <td colspan="6" class="td6">CBR（%）</td>
            </tr>

            <tr trindex="2">
              <td colspan="1" class="td1">1</td>
              <td colspan="3" class="td3" rowspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.jiscs1" disabled />
              </td>
              <td colspan="6" class="td6" rowspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.yasd1" disabled />
              </td>
              <td colspan="6" class="td6" rowspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.cbr1" disabled />
              </td>
            </tr>
            <tr trindex="2">
              <td colspan="1" class="td1">2</td>
            </tr>
            <tr trindex="2">
              <td colspan="1" class="td1">3</td>
            </tr>
            <tr trindex="3">
              <td colspan="1" class="td1">4</td>
              <td colspan="3" class="td3" rowspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.jiscs2" disabled />
              </td>
              <td colspan="6" class="td6" rowspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.yasd2" disabled />
              </td>
              <td colspan="6" class="td6" rowspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.cbr2" disabled />
              </td>
            </tr>
            <tr trindex="3">
              <td colspan="1" class="td1">5</td>
            </tr>
            <tr trindex="3">
              <td colspan="1" class="td1">6</td>
            </tr>
            <tr trindex="4">
              <td colspan="1" class="td1">7</td>
              <td colspan="3" class="td3" rowspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.jiscs3" disabled />
              </td>
              <td colspan="6" class="td6" rowspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.yasd3" disabled />
              </td>
              <td colspan="6" class="td6" rowspan="3">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.cbr3" disabled />
              </td>
            </tr>
            <tr trindex="3">
              <td colspan="1" class="td1">8</td>
            </tr>
            <tr trindex="3">
              <td colspan="1" class="td1">9</td>
            </tr>
            <tr trindex="5">
              <td colspan="16" height="180" class="echarts-border">
                <table-line-log-more-chart :className="'echarts-border'" :chartData="chartData" :minorSplitLine="false"
                  :xMin="chartData.xMin" :xMax="chartData.xMax" :interval="interval"></table-line-log-more-chart>
              </td>
            </tr>

            <tr trindex="6">
              <td colspan="4" class="td4">项目</td>
              <td colspan="4" class="td4">压实度（ 93% )</td>
              <td colspan="4" class="td4">压实度( 94% )</td>
              <td colspan="4" class="td4">压实度( 96% )</td>
            </tr>
            <tr trindex="7">
              <td colspan="4" class="td4">CBR（%）</td>
              <td colspan="4" class="td4">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.yasd93" disabled />
              </td>
              <td colspan="4" class="td4">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.yasd94" disabled />
              </td>
              <td colspan="4" class="td4">
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.yasd96" disabled />
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .....................................................33333333333333333................................................... -->
        <table height="60px" class="bottom-table">
          <tr>
            <td>
              <span v-if="isPdf" class="textarea-span">{{ form.fujsm }}</span>
              <el-input v-else type="textarea" :rows="2" v-model="form.fujsm" @focus="handleFocus('fujsm')" />
            </td>
          </tr>
        </table>
      </div>
      <!-- .................................44444444444............................................. -->
      <table height="22px" class="transparent">
        <tbody>
          <tr>
            <td width="10%" align="right">检测：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">记录：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">复核：</td>
            <td align="left" width="15%"></td>
            <td width="10%">日期：</td>
            <td align="left" width="15%">
              <div style="display: inline-block; margin-left: 20px">年</div>
              <div style="display: inline-block; margin-left: 20px">月</div>
              <div style="display: inline-block ;margin-left: 20px">日</div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
    <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
    <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
  </div>
</template>

<script>
import { handleFocus, emptyConvert, handleKeyDown } from "@/views/reportForms/utils";
import { getTgTdczbjlbsBsInfo, computeTgTdczbjlbsBsInfo } from "@/api/report_tg/tgTdczbjlbsBsInfo";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import TableLineLogMoreChart from "@/views/reportForms/components/TableLineLogMoreChart.vue";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";

import decimal from "@/utils/big-decimal";
import Bus from "@/utils/bus";

export default {
  name: "tdczbjlbs",
  components: {
    BsInstrumentView,
    BsJiancyjView,
    TableLineLogMoreChart,
    BsJudgeBaseView
  },
  props: {
    excelId: {
      type: [String, Number],
      default: () => ""
    },
    syType: {
      type: [String, Number],
      default: () => ""
    },
    page: {
      type: Number,
      default: () => 0
    },
    isPdf: {
      type: Boolean,
      default: () => false
    },
    printEmpty: {
      type: Boolean,
      default: () => false
    }
  },
  data () {
    return {
      // 表单参数
      form: {},
      chartData: {
        xName: "压实度（%）",
        yName: "CBR（%）",
        // xData: [],
        // yData: [],
        seriesData: [],
        time: "",
        xMin: 90,
        xMax: 100,
        yMin: 10,
        yMax: 40,
      },
      interval: 2,
      yInterval: 1,
    };
  },
  watch: {
    isPdf (newVal, oldVal) {
      this.form = { ...emptyConvert(this.form, newVal) };
    }
  },
  created () {
    this.getInfo();
  },
  mounted () {
    // CBR（承载比二获取）
    Bus.$on("cbr2Change", (data) => {
      const { page, jiscs, cbr } = data;
      this.form['jiscs' + (page + 1)] = jiscs;
      this.form['cbr' + (page + 1)] = cbr;
      this.calculateInfo();
    });
    // 干密度（承载比一获取）
    Bus.$on("ganmdpjzChange", (data) => {
      for (const key in data) {
        if (Object.hasOwnProperty.call(data, key)) {
          const element = data[key];
          this.form[key] = element;
        }
      }
      // 计算压实度
      this.calculateYasd();
    });
    // 最大干密度-最佳含水率（引用击实记录） 改为从CBR含水率引入
    Bus.$on("setTgCBRzdgmdPjz", (data) => {
      for (const key in data) {
        if (Object.hasOwnProperty.call(data, key)) {
          const element = data[key];
          this.form[key] = element;
        }
      }
      this.calculateYasd();
    });
  },
  beforeDestroy () {
    // 或者你可以使用其他合适的生命周期钩子
    // 停止监听事件
    Bus.$off("cbr2Change");
    Bus.$off("ganmdpjzChange");
    Bus.$off("setTgCBRzdgmdPjz");
  },
  methods: {
    // 获取详情
    getInfo () {
      getTgTdczbjlbsBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
        this.form = response.data;
        if (!this.form.fujsm) {
          this.form.fujsm = "附加声明：";
        }
        this.setEcharts();
      });
    },
    // 显示检测依据列表
    showBsJiancyjView () {
      this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
    },
    // 检测依据选择
    selectJiancyj (info) {
      this.form.jiancyj = info.label;
      this.form.jiancyjIds = info.ids;
      Bus.$emit("setTgBgJiancyj", { jiancyj: this.form.jiancyj, jiancyjIds: this.form.jiancyjIds });
    },
    // 显示判定依据列表
    showBsJudgeBaseView () {
      this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
    },
    // 判断依据选择
    selectJudge (info) {
      this.form.pandyjIds = info.ids;
      this.form.pandyj = info.label;
    },
    // 显示仪器列表
    showBsInstrumentView () {
      this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
    },
    // 仪器选择
    selectInstrument (info) {
      this.form.zhuyyqsbmcjbh = info.label;
      this.form.shebIds = info.ids;
      Bus.$emit("setTgBgZhuyyqsbmcjbh", { zhuyyqsbmcjbh: this.form.zhuyyqsbmcjbh, shebIds: this.form.shebIds });
    },
    // 计算压实度
    calculateYasd () {
      const zuidgmd = this.form["zuidgmd"];
      for (let index = 1; index <= 3; index++) {
        const ganmd = this.form["ganmd" + index];
        if (zuidgmd && ganmd) {
          //  压实度 = 表一平均干密度 / 最大干密度
          this.form["yasd" + index] = decimal.divide(decimal.multiply(ganmd, 100), zuidgmd);
        }
      }
      this.calculateInfo();
    },
    // 计算
    calculateInfo () {
      for (let index = 1; index <= 3; index++) {
        const yasd = this.form["yasd" + index];
        const cbr = this.form["cbr" + index];
        if (!yasd || !cbr) {
          return;
        }
      }
      computeTgTdczbjlbsBsInfo(this.form).then((response) => {
        this.form = response.data;
        console.log(this.form);
        this.setEcharts();
        this.setTgBgJiancjg();
      });
    },
    // 给报告传值
    setTgBgJiancjg () {
      Bus.$emit("setTgBgJiancjg", [
        { key: 14, value: this.form.yasd93 },
        { key: 15, value: this.form.yasd94 },
        { key: 16, value: this.form.yasd96 }
      ]);
    },
    // 拼接图表数据
    setEcharts () {
      if (!this.form.chartData || this.form.chartData.length <= 0) {
        return;
      }
      let data1 = [];
      let data2 = [];
      let data3 = [];
      let xMin = 0;
      let xMax = 0;
      let yMin = 0;
      let yMax = 0;
      this.form.chartData.forEach((item, index) => {
        if (index == 0) {
          xMin = item.point[0];
          xMax = item.point[0];
          yMin = item.point[1];
          yMax = item.point[1];
        }
        if (item.point[0] < xMin) {
          xMin = item.point[0];
        }
        if (item.point[1] < yMin) {
          yMin = item.point[1];
        }
        if (item.point[0] > xMax) {
          xMax = item.point[0];
        }
        if (item.point[1] > yMax) {
          yMax = item.point[1];
        }
        switch (item.flag) {
          case "100":
            data1.push(item.point);
            break;
          case "010":
            data2.push(item.point);
            break;
          case "011":
            data3.push(item.point);
            break;
        }
      });
      xMin = xMin > 0 ? decimal.subtract(xMin, this.interval, 0) : xMin;
      yMin = yMin > 0 ? decimal.subtract(yMin, this.yInterval, 0) : yMin;
      xMax = 100;
      yMax = decimal.add(yMax, this.yInterval, 0);
      this.chartData.xMin = xMin;
      this.chartData.yMin = yMin;
      this.chartData.xMax = xMax;
      this.chartData.yMax = yMax;
      let markLineData = [];
      data3.forEach((item) => {
        markLineData.push([
          {
            coord: [90, item[1]]
          },
          {
            coord: [item[0], item[1]]
          }
        ]);
        markLineData.push([
          {
            coord: [item[0], 0]
          },
          {
            coord: [item[0], item[1]]
          }
        ]);
      });
      const labelData = data3.map((item) => {
        return {
          value: item,
          label: {
            show: true,
            formatter: JSON.stringify(item),
            position: "top"
          },
          itemStyle: {}
        };
      });
      const chart1 = {
        data: data1,
        connectNulls: true,
        smooth: true,
        symbol: "none",
        type: "line"
      };
      const chart2 = {
        type: "scatter",
        symbolSize: 4,
        data: [...data2, ...labelData],
        markLine: {
          symbolSize: [1],
          label: { show: true },
          silent: true,
          data: markLineData
        }
      };
      const chart3 = {
        type: "scatter",
        symbolSize: 4,
        data: [[90, 0]],
        symbol: "none",
        markLine: {
          symbolSize: [1],
          label: { show: true },
          silent: true
        }
      };
      this.chartData.seriesData = [chart1, chart2, chart3];
    },
    submitForm () {
      return this.form;
    },
    handleFocus (event) {
      handleFocus(event, this.page, this.form[event]);
    },
    handleKeyDown (event, rowIndex, colIndex) {
      const maxArr = [5, 5]; // 每一行最多有多少可聚焦输入框（从0开始）
      const ref = handleKeyDown(event, rowIndex, colIndex, maxArr);
      if (this.$refs[ref]) {
        this.$refs[ref].focus();
      }
    }
  }
};
</script>

<style scoped>
.td1 {
  width: calc(100% / 16 * 1);
}

.td2 {
  width: calc(100% / 16 * 2);
}

.td3 {
  width: calc(100% / 16 * 3);
}

.td4 {
  width: calc(100% / 16 * 4);
}

.td5 {
  width: calc(100% / 16 * 5);
}

.td6 {
  width: calc(100% / 16 * 6);
}

.echarts-border {
  width: calc(210mm - 40mm - 8px);
  height: 176px;
  padding: 2px !important;
  background-color: #ffffff !important;
}
</style>
